/**
 * GoogleTranslationServiceImpl.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation.services;

import java.net.Authenticator;
import java.util.*;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

import extjsdyntran.servlets.Constants;
import extjsdyntran.translation.ServiceTypes;
import extjsdyntran.translation.TranslationServiceAbstract;
import extjsdyntran.translation.store.StoreManager;

/**
 * @description Translation service loading translations from Google Translation Service
 */
public final class GoogleTranslationServiceImpl extends TranslationServiceAbstract {

	private static final long serialVersionUID = 1L;

	public GoogleTranslationServiceImpl(String username, String password,	String _path) {
		super(username, password, _path);
		loadLanguages();
	}


	public boolean isNewLanguageSupported(){
		return false;
	}

	public ServiceTypes isLanguageSupported(String language){
		try{
			Language.valueOf(language);
			return  ServiceTypes.NORMAL  ;
		}catch(Exception e){
			return null;	
		}		 
	}
	
	// load languages from stored files
	public void loadLanguages() {
		logger.info("Loading translations for Google Translation Service");
		super.loadLanguages(super.enumNameToStringArray(Language.values()));
	}

	public Map getListOfLanguages() {		
		Map langs = new HashMap();
		Language [] lg = Language.values();
		for (int i = 0; i < lg.length; i++) {
			if (lg[i].name().startsWith("AUTO"))
				continue;
			langs.put(lg[i].name(), lg[i].toString());
		}
				
		return langs;
	}
	

	public String translate(String text, String language) {
		try {

			Properties lang = (Properties) translations.get(language);
			String result = null;
			if (lang == null) {
				lang = new Properties();
				translations.put(language, lang);
			} else	result = (String) lang.get(text);
			
			if (result == null) {
				try{
					result = Translate.translate(text, Language.ENGLISH, Language.valueOf(language));
					lang.setProperty(text, result);
					StoreManager.saveTranslation( language, text, result);					
				}catch (Exception e){
					lang.setProperty(text, text);
					result = text;
					logger.error("Error remote translation for language {} and value: {}", language, text);
					logger.error("",e.getMessage());
				}
				
			}

			// UTF-8
			// PrintStream out = new PrintStream(System.out, true, "UTF-8");
			// out.println(result);

			return result;
		} catch (Exception e) {
			logger.error("Error trying to translate for language {} and value : {}", text, language);
			logger.error("",e);
			return Constants.json_false;
		}

	}
	

	public boolean test() {
		try {
			// PROXY IS SET IN net.properties in JAVA RUNTIME
			// Properties systemProperties = System.getProperties();
			// systemProperties.setProperty("http.proxyHost",proxy);
			// systemProperties.setProperty("http.proxyPort",port);

			// If proxy is set, default autosignon user must be set for server
			// code
			// @update - browser requests username and password and pass it
			// through
			if ((username!=null) && (password!=null)) {
				ProxyAuthenticator sa = new ProxyAuthenticator(username,password);
				Authenticator.setDefault(sa);
			}

			Translate.translate("Hello", Language.ENGLISH, Language.CROATIAN);
			return true;
		} catch (Exception ex) {
			logger.error("Error trying to test Google Translation Service", ex);
			return false;
		}

	}

	public ServiceTypes getServiceType(){
		return ServiceTypes.NORMAL;
	}

	public boolean removeLanguageFromCache(String language){
		
		if (!translations.containsKey(language)) {
			logger.warn("Error removing null language!");
			return false;
		}	
		
		logger.info("Removing language {} from storage!",language);
		if (StoreManager.clearTranslation(this,language)){
			translations.remove(language);
			logger.info("Language {} removed from storage!",language);
			return true;	
		}
		logger.warn("Language {} NOT removed from storage!",language);
		return false;		
	}
	
	public boolean appendLanguageToCache(String language){
		return false;	
	}
	
}